package com.ruoyi.lock.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.lock.domain.AInbound;
import com.ruoyi.lock.domain.ASmartlocks;
import com.ruoyi.lock.mapper.AInboundMapper;
import com.ruoyi.lock.service.IAInboundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 入库单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-15
 */
@Service
public class AInboundServiceImpl implements IAInboundService 
{
    @Autowired
    private AInboundMapper aInboundMapper;


    /**
     * 查询入库单
     * 
     * @param iId 入库单主键
     * @return 入库单
     */
    @Override
    public AInbound selectAInboundByIId(Long iId)
    {
        return aInboundMapper.selectAInboundByIId(iId);
    }

    /**
     * 查询入库单列表
     * 
     * @param aInbound 入库单
     * @return 入库单
     */
    @DataScope(deptAlias = "d")
    @Override
    public List<AInbound> selectAInboundList(AInbound aInbound)
    {
        return aInboundMapper.selectAInboundList(aInbound);
    }

    /**
     * 新增入库单
     * 
     * @param aInbound 入库单
     * @return 结果
     */
    @Override
    public int insertAInbound(AInbound aInbound)
    {
        aInbound.setDeptId(SecurityUtils.getDeptId());

        aInbound.setCreateTime(DateUtils.getNowDate());
        aInbound.setInboundTime(DateUtils.getNowDate());
        return aInboundMapper.insertAInbound(aInbound);
    }

    /**
     * 修改入库单
     * 
     * @param aInbound 入库单
     * @return 结果
     */
    @Override
    public int updateAInbound(AInbound aInbound)
    {
        aInbound.setUpdateTime(DateUtils.getNowDate());
        return aInboundMapper.updateAInbound(aInbound);
    }

    /**
     * 批量删除入库单
     * 
     * @param iIds 需要删除的入库单主键
     * @return 结果
     */
    @Override
    public int deleteAInboundByIIds(Long[] iIds)
    {
        return aInboundMapper.deleteAInboundByIIds(iIds);
    }

    /**
     * 删除入库单信息
     * 
     * @param iId 入库单主键
     * @return 结果
     */
    @Override
    public int deleteAInboundByIId(Long iId)
    {
        return aInboundMapper.deleteAInboundByIId(iId);
    }

    /**
     * 产品出库
     * */
    @Override
    @Transactional
    public int productsOutbound(ASmartlocks aSmartlocks) {
        //新增设备里面的数据
//        aSmartlocks.setStatus("2");//默认状态 “待安装”
//        int aSmartlocksResult = aSmartlocksService.insertASmartlocks(aSmartlocks);
//        //如果成功了就修改库存表
//        if(aSmartlocksResult > 0){
//            AInbound aInbound1 = new AInbound();
//            aInbound1.setOutboundType(1L);
//            aInbound1.setOutboundTime(aSmartlocks.getOutboundTime());
//            aInbound1.setMac(aSmartlocks.getMac());
//            aInboundMapper.updateInboundData(aInbound1);
//        }

        return 0;
    }

    /**
     * 剩余数/闲置数
     * @return
     */
    @Override
    public int getIdleQuantity()
    {
        return aInboundMapper.getIdleQuantity();
    }


    /**
     * 收回产品
     * @return
     */
    @Override
    @Transactional
    public int recallLock(String mac) {
//        int recallLockResult = 0;
//        //删除待安装的锁设备
//        ASmartlocks aSmartlocks = aSmartlocksMapper.selectLockIdByMac(mac);
//        if(aSmartlocks != null) {
//            if (aSmartlocks.getStatus().equals("2")) {
//                int result = aSmartlocksMapper.deleteASmartlocksById(aSmartlocks.getId());
//                if (result > 0) {
//                    //根据锁编号修改产品状态
//                    recallLockResult = aInboundMapper.updateLockStatusRecallLock(mac);
//                } else {
//                    recallLockResult = 3;
//                }
//            } else {
//                recallLockResult = 0;
//            }
//        }else{
//            recallLockResult = 4;
//        }
        return 0;

    }
}
